//
//  ViewController.m
//  11_Work
//
//  Created by dllo on 16/7/27.
//  Copyright © 2016年 yh. All rights reserved.
//

#import "ViewController.h"
#define kTableViewReuseFirst @"firstReuse"
#define kTableViewReuseThree @"threeReuse"
#import "UIImageView+WebCache.h"

#import "FirstImage.h"
#import "OneImageTableViewCell.h"

#import "ThreePhoto.h"
#import "ThreePhotoTableViewCell.h"

@interface ViewController () <UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSDictionary *dataDic;
@property (nonatomic, strong) NSMutableArray *firstImageArr;
@property (nonatomic, strong) NSMutableArray *threeArr;
@property (nonatomic, strong) NSArray *arr;

@property (nonatomic, strong) UIButton *firstButton;
@property (nonatomic, strong) UIButton *secButton;
@property (nonatomic, strong) UIButton *thiredButton;
@property (nonatomic, strong) UIButton *fourthButton;

@property (nonatomic, strong) UIScrollView *scrollView;
@end

@implementation ViewController

- (void)viewDidLoad {
    self.navigationController.navigationBar.translucent = NO;
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self createTableView];
    [self createData];
    [self createHeadView];
   
}

- (void)createHeadView {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200)];
    self.tableView.tableHeaderView = view;
    _firstButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_firstButton setImage:[UIImage imageNamed:@"1"] forState:UIControlStateNormal];
    _firstButton.frame = CGRectMake(5, 105, self.tableView.tableHeaderView.frame.size.width / 4 - 10, 90);
    [self.tableView.tableHeaderView addSubview:_firstButton];
    
    _secButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_secButton setImage:[UIImage imageNamed:@"2"] forState:UIControlStateNormal];
    _secButton.frame = CGRectMake(self.tableView.tableHeaderView.frame.size.width / 4 + 5, 105, self.tableView.tableHeaderView.frame.size.width / 4 - 10, 90);
    [self.tableView.tableHeaderView addSubview:_secButton];
    
    _thiredButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_thiredButton setImage:[UIImage imageNamed:@"3"] forState:UIControlStateNormal];
    _thiredButton.frame = CGRectMake(self.tableView.tableHeaderView.frame.size.width / 2 + 5, 105, self.tableView.tableHeaderView.frame.size.width / 4 - 10, 90);
    [self.tableView.tableHeaderView addSubview:_thiredButton];
    
    _fourthButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_fourthButton setImage:[UIImage imageNamed:@"4"] forState:UIControlStateNormal];
    _fourthButton.frame = CGRectMake(self.tableView.tableHeaderView.frame.size.width / 4 * 3 + 5, 105, self.tableView.tableHeaderView.frame.size.width / 4 - 10, 90);
    [self.tableView.tableHeaderView addSubview:_fourthButton];

    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 100)];
    self.scrollView.contentSize = CGSizeMake(4 * self.view.frame.size.width, 100);
    self.scrollView.backgroundColor = [UIColor redColor];
    [self.tableView.tableHeaderView addSubview:self.scrollView];
    self.scrollView.pagingEnabled = YES;
}

- (void)createData {
    NSString *path = [[NSBundle mainBundle] pathForResource:@"YiChe" ofType:@"plist"];
    self.dataDic = [NSDictionary dictionary];
    self.dataDic = [NSDictionary dictionaryWithContentsOfFile:path];
    self.firstImageArr = [NSMutableArray array];
    self.firstImageArr = [NSMutableArray array];
    for (NSDictionary *dic in self.dataDic[@"data"][@"list"]) {
        if (dic[@"type"] == [NSNumber numberWithInteger:3]) {
            NSString *str = dic[@"picCover"];
            NSArray *arr = [str componentsSeparatedByString:@";"];
            ThreePhoto *three = [[ThreePhoto alloc] initWithLeft:arr[0] mid:arr[1] right:arr[2] style1:[NSNumber numberWithInteger:3]];
            [self.firstImageArr addObject:three];
        } else{
            FirstImage *firstImage = [[FirstImage alloc] initWithTitle:dic[@"title"] style1:dic[@"type"] pic:dic[@"picCover"]];
//            FirstImage *firstImage = [[FirstImage alloc] init];
//            firstImage.title = dic[@"title"];
            [self.firstImageArr addObject:firstImage];
        }}
}

- (void)createTableView {
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    self.tableView.rowHeight = 80;
    [self.tableView registerClass:[OneImageTableViewCell class] forCellReuseIdentifier:kTableViewReuseFirst];
    
    [self.tableView registerClass:[ThreePhotoTableViewCell class] forCellReuseIdentifier:kTableViewReuseThree];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.firstImageArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([self.firstImageArr[indexPath.row] style1] == [NSNumber numberWithInteger:3]) {
//    if ([self.firstImageArr[indexPath.row] isKindOfClass:[ThreePhoto class]]) {
    
        ThreePhotoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kTableViewReuseThree];
        cell.threePhoto = self.firstImageArr[indexPath.row];
        return cell;
    } else {
    OneImageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kTableViewReuseFirst];
        cell.firstImage = self.firstImageArr[indexPath.row];
        
//        [cell.firstImage sd_setImageWithURL:[NSURL URLWithString:self.firstImageArr[indexPath.row]]];
        //    self.firstImageArr[indexPath.row];
    return cell;
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
